<template>
	<view>
		<view class="container " v-if="config && config.config">
			<view class="account" v-if="open_multilingual">
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="多语言"></lang>
					</view>
					<view class="fr">{{ now_lang.display }}</view>
					<form :report-submit="true" class="formidForm" @submit="choose_language">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
			</view>
			<view class="account">
				<!-- <view class="list clearfix">
					<view class="fl">
						<lang keywords="昵称"></lang>
					</view>
					<view class="fr">{{userInfo.nickname}}</view>
					<form :report-submit="true" class="formidForm" @submit="go_username">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view> -->
				<view class="list clearfix" v-if="userInfo.phone !=''">
					<view class="fl">
						<lang keywords="修改手机号"></lang>
					</view>
					<view class="fr">{{userInfo.showPhone}}</view>
					<form :report-submit="true" class="formidForm" @submit="go_number">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
				<view class="list clearfix" v-else>
					<view class="fl">
						<lang keywords="绑定手机号"></lang>
					</view>
					<form :report-submit="true" class="formidForm" @submit="go_newnumber">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="收货地址"></lang>
					</view>
					<form :report-submit="true" class="formidForm" @submit="go_address">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="我的银行卡"></lang>
					</view>
					<form :report-submit="true" class="formidForm" @submit="goMyCard($event)" :data-url="'url/Fenrun/bank_list&now_lang='+now_lang.val"
					 data-url_title="我的银行卡">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
				<block v-if="config.config.user_is_real_name">
					<view class="list clearfix">
						<view class="fl">
							<lang keywords="实名认证"></lang>
						</view>
						<form :report-submit="true" class="formidForm" @submit="goUrl($event)" data-url="url/My/authentication_index"
						 data-url_title="实名认证">
							<button formType="submit" class="formidBtn"></button>
						</form>
					</view>
					<view class="list clearfix">
						<view class="fl">
							<lang keywords="车主认证"></lang>
						</view>
						<form :report-submit="true" class="formidForm" @submit="goUrl($event)" data-url="url/My/car_apply" data-url_title="车主认证">
							<button formType="submit" class="formidBtn"></button>
						</form>
					</view>
				</block>
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="我的生日"></lang>
					</view>
					<view class="fr">
						<block v-if="userInfo && userInfo.birthday && userInfo.birthday != '0000-00-00'">
							{{ userInfo.birthday }}
						</block>
						<block v-else>
							<lang keywords="未设置"></lang>
						</block>
					</view>
					<form :report-submit="true" class="formidForm" @submit="goBirth">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="我的邮箱"></lang>
					</view>
					<view class="fr">
						<block v-if="userInfo && userInfo.showEmail && userInfo.showEmail != ''">
							{{ userInfo.showEmail }}
						</block>
						<block v-else>
							<lang keywords="未设置"></lang>
						</block>
					</view>
					<form :report-submit="true" class="formidForm" @submit="goUrl($event)" data-url="url/My/email" data-url_title="我的邮箱">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="注销账号"></lang>
					</view>
					<view class="fr"></view>
					<form :report-submit="true" class="formidForm" @submit="resetAccount">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
			</view>
			<view class="account">
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="关于我们"></lang>
					</view>
					<form :report-submit="true" class="formidForm" @submit="goUrl($event)" data-url="url/My/about" data-url_title="关于我们">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
			</view>
			<!-- <view class="account">
				<view class="list clearfix">
					<view class="fl">
						<lang keywords="管理店铺"></lang>
					</view>
					<form :report-submit="true" class="formidForm" @submit="goUrl($event)" data-url="/packapp/merchant/index.html"
					 data-url_title="管理店铺">
						<button formType="submit" class="formidBtn"></button>
					</form>
				</view>
			</view> -->
			<button class="block_btn mlr20" @tap="relogin" :style="'background:' + config.config.mobile_head_color">
				<lang v-if="!isBrowser" keywords="重新登录"></lang>
				<lang v-if="isBrowser" keywords="退出登录"></lang>
			</button>
		</view>

		<view class="version">[[[wxapp_version]]]</view>
	</view>
</template>

<script>
	import common from '../../utils/common.js'
	var app = getApp();
	var _this;
	
	console.log('app')
	console.log(app)

	export default {
		data() {
			return {
				userInfo: {},
				config: {},
				now_lang: {},
				open_multilingual: false,
				mobile_head_color: '',
				isBrowser: false
			};
		},

		onShow: function() {
			// #ifdef MP-WEIXIN || H5
			app.globalData.userInfo = null;
			common.getUserInfo(function(userInfo) {
				_this.userInfo = userInfo
				console.log(_this.userInfo);
			});
			// #endif
			
			// // #ifdef H5
			// console.log('个人信息', app.globalData.userInfo)
			// _this.userInfo = app.globalData.userInfo
			// // #endif
			
			// #ifdef H5
			common.hideWxShare()
			// #endif
		},
		onLoad: function() {
			_this = this;
			common.setMainColor(_this, function(config) {
				console.log('222222111', config)
				if (app.globalData.open_multilingual && config.config.open_multilingual == '1') {
					_this.open_multilingual = true
					var lang_list = _this.config.config.lang_config.lang_list;
					var show_lang_list = [];

					for (var i in lang_list) {
						if (lang_list[i].val == config.config.lang_config.now_lang) {
							_this.now_lang = {
								display: lang_list[i].display,
								val: lang_list[i].val
							}

							break;
						}
					}
				}
				
				wx.setNavigationBarTitle({
					title: common.changeTxt('账户管理')
				});
			}); 
			// #ifdef H5
			if (!common.$checkPlat.checkWeixin() && !common.$checkPlat.checkAlipay()) {
				this.isBrowser = true
			}
			// #endif
			
		},
		methods: {
			choose_language(e) { 
				console.log(e);

				if (e.type == 'submit') {
					common.upFormid(e);
				}

				var lang_list = _this.config.config.lang_config.lang_list;
				var show_lang_list = [];

				for (var i in lang_list) {
					show_lang_list.push(lang_list[i].display);
				}

				wx.showActionSheet({
					itemList: show_lang_list,

					success(res) {
						_this.now_lang = {
							display: lang_list[res.tapIndex].display,
							val: lang_list[res.tapIndex].val
						}

						var city_param = {};

						if (_this.config) { 
							city_param.now_city = _this.config.city.top_city ? _this.config.city.top_city.area_id : _this.config.city.area_id;
							city_param.now_area = _this.config.city.area_id;
						}

						wx.setStorageSync('city_param', city_param);
						wx.setStorageSync('now_lang', lang_list[res.tapIndex].val);
						app.globalData.config = null;
						app.globalData.languageData = null;
						app.globalData.tabBar = null;
						wx.removeStorageSync('wxapp_config');
						app.getConfig();
						var have_config = setInterval(() => {
							// wx.showLoading({
							//   title: '',
							// })
							if (app.globalData.config != null) {
								clearInterval(have_config); // wx.hideLoading()

								wx.reLaunch({
									url: '/pages/plat_menu/index'
								});
							}
						}, 100);
					},

					fail(res) {
						console.log(res.errMsg);
					}

				});
			},

			goMyCard(e) { 
				if (_this.userInfo.phone == '') {
					wx.showModal({
						title: common.changeTxt('提示'),
						content: common.changeTxt('请先绑定手机号'),
						showCancel: false,
						success: function() {
							wx.navigateTo({
								url: '/pages/my/newnumber'
							});
						}
					});
					return false;
				}
				 
				this.goUrl(e);
			},
			
			// 注销账号
			resetAccount (e) {
				if (e.type == 'submit') {
					common.upFormid(e);
				}
				uni.showModal({
					title: common.changeTxt('提示'),
					content: common.changeTxt('确定要注销当前账号？注销后不可恢复'),
					cancelText: common.changeTxt('取消'),
					confirmText: common.changeTxt('确定'),
					success: (res) => {
						if (res.confirm) {
							common.post('Login&a=cancel', {}, (data) => {
								console.log('注销账号成功', data)
								// #ifdef H5
								common.$dataStorage.cookie.set('login_status', 'logout')
								window.localStorage.removeItem('packapp_ticket')
								window.localStorage.removeItem('packapp_user')
								window.localStorage.removeItem('packapp_device')
								uni.removeStorageSync('device')
								// #endif
								
								uni.removeStorageSync('ticket')
								app.globalData.userInfo = ''
								app.globalData.is_login = false
								
								common.getTabBar(function(tabBar){
									console.log('获取到tabBar', tabBar)
									let homePage = tabBar.list[0]
									if (homePage.isSpecial) {
										if (tabBar.list.length == 1) {
											app.globalData.tabWeb = {
												url: homePage.pagePath,
												title: homePage.text
											};
											uni.switchTab({
												url: '/pages/webview/tab_webview'
											});
										}
										
										if (homePage.pageType == 'page') {
											uni.reLaunch({
												url: homePage.pagePath
											})
										} else {
											common.parseUrl(homePage.pagePath, '', false, homePage.text);
										}
									} else {
										uni.switchTab({url: homePage.pagePath})
									}
								})
							})
						}
					}
				})
				
			},

			goUrl(e) {
				 
				console.log(e);

				if (e.type == 'submit') {
					common.upFormid(e);
				}

				var pageTitle = e.currentTarget.dataset.url_title;

				if (!pageTitle) {
					pageTitle = '';
				}

				common.getTxtKey(pageTitle).then(key => {
					common.parseUrl(e.currentTarget.dataset.url, '', false, key);
				}, () => {});
			},

			go_address: function(e) {
				console.log(e);

				if (e.type == 'submit') {
					common.upFormid(e);
				}

				wx.navigateTo({
					url: "address"
				});
			},
			go_username: function(e) {
				console.log(e);

				if (e.type == 'submit') {
					common.upFormid(e);
				}

				wx.navigateTo({
					url: "username"
				});
			},
			go_number: function(e) {
				console.log(e);

				if (e.type == 'submit') {
					common.upFormid(e);
				}

				wx.navigateTo({
					url: "number"
				});
			},
			go_newnumber: function(e) {
				console.log(e);

				if (e.type == 'submit') {
					common.upFormid(e);
				}

				wx.navigateTo({
					url: "newnumber"
				});
			},

			goBirth(e) {
				if (e.type == 'submit') {
					common.upFormid(e);
				}

				wx.navigateTo({
					url: "birth?birth=" + this.userInfo.birthday
				});
			},

			relogin: function() {
				app.logout();
				if (this.isBrowser) {
					common.getTabBar((tabbar) => {
						if (tabbar.list[0].pagePath.indexOf('/pages/') == 0) {
							wx.reLaunch({
								url: tabbar.list[0].pagePath
							});
						} else {
							if (tabbar.list.length > 1) {
								common.parseUrl(tabbar.list[0].pagePath);
							} else {
								tabbar.hidden = true;
								app.globalData.tabWeb = {
									url: tabbar.list[0].pagePath,
									title: tabbar.list[0].text
								};
								wx.reLaunch({
									url: '/pages/webview/tab_webview'
								});
							}
						}
					})
				} else {
					wx.navigateBack();
				}
			}
		}
	};
</script>
<style>
	.container {
		padding-bottom: 40rpx;
		margin-bottom: 60rpx;
		position: relative;
		z-index: 11;
		background-color: #f4f4f4;
	}

	.account {
		padding: 0 20rpx;
		background: #fff;
		margin-top: 20rpx;
	}

	.account .list {
		position: relative;
		font-size: 28rpx;
		line-height: 40rpx;
		height: 40rpx;
		padding: 24rpx 34rpx 24rpx 10rpx;
		border-bottom: 2rpx solid #f1f1f1;
	}

	.account .list:last-child {
		border-bottom: none;
	}

	.account .list:after {
		display: block;
		content: "";
		border-top: 2rpx solid #BFBFBF;
		border-left: 2rpx solid #BFBFBF;
		width: 20rpx;
		height: 20rpx;
		-webkit-transform: rotate(135deg);
		background-color: transparent;
		position: absolute;
		top: 50%;
		right: 10rpx;
		margin-top: -10rpx;
	}

	.version {
		position: fixed;
		bottom: 10px;
		left: 10px;
		right: 10px;
		color: #ddd;
		text-align: center;
		font-size: 12px;
	}
</style>
